// pages/goods_list/index.js
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodList: [],
    tabs: [
      {
        id: 0,
        name: '综合',
        isActive: false,
      },
      {
        id: 1,
        name: '销量',
        isActive: true,
      },
      {
        id: 2,
        name: '价格',
        isActive: false,
      },
    ],
    queryParams: {
      query: '',
      cid: '',
      pagenum: 1,
      pagesize: 10,
    },
    totalPage: 1,
    emptyImage:
      'https://ww1.sinaimg.cn/large/007rAy9hgy1g24by9t530j30i20i2glm.jpg',
    isBottom: false,
    isShow: false,
    scrollTop: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { cid } = options;
    const cat_id = parseInt(cid);
    this.setData({
      queryParams: {
        query: '',
        pagenum: 1,
        pagesize: 10,
        cid: cat_id,
      },
    });
    // this.getList();
    this.getTotalNum(cat_id);
    this.getGoodsList(cat_id);
  },
  // 云函数获取当前分类商品总数
  getTotalNum(cat_id) {
    const that = this;
    wx.cloud
      .callFunction({
        name: 'goods',
        data: {
          type: 'getCount',
          cid: cat_id,
        },
      })
      .then((res) => {
        const { total } = res.result;
        const { pagesize } = that.data.queryParams;
        const totalPage = Math.ceil(total / pagesize);
        that.setData({
          totalPage,
        });
      });
  },
  // 云函数获取商品列表
  getGoodsList(cat_id) {
    let that = this;
    const { pagenum, pagesize } = that.data.queryParams;
    wx.cloud
      .callFunction({
        name: 'goods',
        data: {
          type: 'getGoodsList',
          pagenum: pagenum,
          pagesize: pagesize,
          cid: cat_id,
        },
      })
      .then((res) => {
        const goods = res.result.data;
        that.setData({
          goodList: [...that.data.goodList, ...goods],
        });

        // 关闭下拉刷新的窗口
        wx.stopPullDownRefresh();
      })
      .catch((err) => {
        console.log(err);
      });
  },
  // 接口获取商品列表
  getList() {
    app.wxRequest({
      url: '/goods/search',
      data: this.data.queryParams,
    })
      .then((res) => {
        const { goods } = res.data.message;
        const { total } = res.data.message;
        const totalPage = Math.ceil(total / this.data.queryParams.pagesize);

        this.setData({
          goodList: [...this.data.goodList, ...goods],
          totalPage: totalPage,
        });

        // 关闭下拉刷新的窗口
        wx.stopPullDownRefresh();
      })
      .catch((err) => {
        console.log(err);
      });
  },
  // 顶部tab切换
  tabChange(e) {
    const { index } = e.detail;
    const { tabs } = this.data;
    tabs.forEach((val, i, arr) => {
      index === i ? (val.isActive = true) : (val.isActive = false);
    });
    this.setData({
      tabs: tabs,
    });
  },

  // 重置数据
  reset() {
    const { cid } = this.data.queryParams;
    this.setData({
      goodList: [],
      queryParams: {
        query: '',
        cid: cid,
        pagenum: 1,
        pagesize: 10,
      },
      isBottom: false,
    });
    // this.getList();
    this.getGoodsList(cid);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.reset();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.queryParams.pagenum >= this.data.totalPage) {
      this.setData({
        isBottom: true,
      });
    } else {
      const { pagenum } = this.data.queryParams;
      const { cid } = this.data.queryParams;
      const currNum = pagenum + 1;
      this.setData({
        queryParams: {
          query: '',
          cid: cid,
          pagenum: currNum,
          pagesize: 10,
        },
      });
      // this.getList();
      this.getGoodsList(cid);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  /**
   * 页面滚动监听
   * @param {*} e
   */
  onPageScroll: function (e) {
    let { isShow } = this.data;
    if (e.scrollTop > 60) isShow = true;
    else isShow = false;
    this.setData({
      isShow,
    });
  },
});
